#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys
from faker import Faker
import logging
import random
import sqlite3
from datelabel.name import RandomName
from datelabel.birthday import birthday
from datelabel.get_nationality import *


class OperatorSqlite(object):
    def __init__(self, db_path="test.db"):
        self.conn = sqlite3.connect(db_path)
        print("Opened database successfully")
        self.cur = self.conn.cursor()

    def execute_sql(self, sql):
        return self.cur.execute(sql)

    def create_table(self):
        sql = "CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY,name TEXT,birth TEXT,gender TEXT check(gender in('男', '女'))," \
              "nationality TEXT, country TEXT, addrerss TEXTVARCHAR(255), phone TEXT, email VARCHAR(255), profession TEXT, company TEXT, marriage TEXT, religion TEXT)"
        print(self.execute_sql(sql))

    def add_date(self, data):
        sql = 'INSERT INTO test VALUES (%s)' % data
        print(sql)
        print(self.execute_sql(sql))

    def select_date(self):
        sql = 'select * from Test'
        self.execute_sql(sql)

    def close_con(self):
        self.conn.commit()
        self.conn.close

def random_sex():
    male = "男"
    fmale = "女"
    rse = lambda sex: male if sex == 1 else fmale
    return rse(random.randint(0, 1))


def random_marriage():
    male = "已婚"
    fmale = "未婚"
    rse = lambda sex: male if sex == 1 else fmale
    return rse(random.randint(0, 1))


def my_address():
    my_address = f.address()
    aa = my_address.split(' ')
    l = random.randint(1, 10)
    ll = random.randint(1, 10)
    lll = random.randint(1, 100)
    aaa = aa[0] + str(l) + '栋' + str(ll) + '单元' + str(lll) + '室'
    return aaa

if __name__ == "__main__":
    print('程序名称为：sqlite，第一个参数为：{}，第二个参数为：{}'.format(sys.argv[1], sys.argv[2]))
    num = int(sys.argv[1])
    db_name = sys.argv[2]
    f = Faker(locale='zh_CN')
    os = OperatorSqlite(db_name)
    os.create_table()
    rn = RandomName()
    for i in range(0, num):
        name = rn.random_name_str()
        birth = birthday()
        gender = random_sex()
        nationality = random_nation()
        country = get_nationality()

        address = my_address()

        phone = f.phone_number()
        email = f.free_email()
        perfession = f.job()
        company = f.company()
        marriage = random_marriage()
        religion = random_religious()
        data = str(i) + ",'" + name + "','" + birth + "','" + gender + "','" + nationality + "','" + country + "','" + address  \
                + "','" + phone + "','" + email + "','" + perfession + "','" + company + "','" + marriage + "','" + religion + "'"
        # print(data)
        os.add_date(data)
    os.close_con()
